package com.tencent.miniproject.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.tencent.miniproject.R;
import com.tencent.miniproject.adapter.NeighborRecommandAdapter;
import com.tencent.miniproject.base.BaseFragment;
import com.tencent.miniproject.been.NeighborRecommand;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.fragment.i.INeighborFriendsFragment;
import com.tencent.miniproject.listener.OnClickItemVoteListener;
import com.tencent.miniproject.presenter.NeighborFriendsPresenter;
import com.tencent.miniproject.presenter.i.INeighborFriendsPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class NeighborFriendsFragment extends BaseFragment implements INeighborFriendsFragment {
    private static final String TAG = "NeighborFriendsFragment";
    @BindView(R.id.rv_content)
    RecyclerView mRvContent;

    int mFragmentType;
    @BindView(R.id.srl_refresh)
    SwipeRefreshLayout mSrlRefresh;
    private NeighborRecommandAdapter mRecommandAdapter;
    private List<NeighborRecommand> mRecommandList;
    private INeighborFriendsPresenter mPresenter;
    private int mPage = 1;
    private String mTag;
    private Double mLongitude;
    private Double mLatitude;

    public static NeighborFriendsFragment newInstance(int type, String tag, Double longitude, Double latitude) {
        NeighborFriendsFragment fragment = new NeighborFriendsFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        if (type == 0) {
            args.putString("tag", tag);
        } else {
            args.putDouble("longitude", longitude);
            args.putDouble("latitude", latitude);
        }
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutResourceId() {
        return R.layout.fragment_neighbor_friends;
    }

    @Override
    protected void initData(Bundle bundle) {
        if (getArguments() != null) {
            mFragmentType = getArguments().getInt("type");
            if (mFragmentType == 0) {
                mTag = getArguments().getString("tag");
            } else {
                mLongitude = getArguments().getDouble("longitude");
                mLatitude = getArguments().getDouble("latitude");
            }
        }
        mRecommandList = new ArrayList<>();
        mRecommandAdapter = new NeighborRecommandAdapter(mBaseActivity,mLongitude,mLatitude);
    }

    @Override
    protected void initView() {
        mRvContent.setLayoutManager(new LinearLayoutManager(mBaseActivity, LinearLayoutManager.VERTICAL, false));
        mRvContent.setItemAnimator(new DefaultItemAnimator());
        mRecommandAdapter.updateData(mRecommandList);
        mRvContent.setAdapter(mRecommandAdapter);
        mPresenter = new NeighborFriendsPresenter(this);
        mPresenter.getPostData(mFragmentType, mLongitude, mLatitude, mPage, null);
    }

    @Override
    protected void setListener() {
        mRecommandAdapter.setOnClickVoteListener(new OnClickItemVoteListener() {
            @Override
            public void onClick(int position) {

            }
        });
        if (mFragmentType == 0) {
            mPresenter.getPostData(Constant.FRAGMENT_TAG, null, null, mPage, mTag);
        }
        mSrlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSrlRefresh.setRefreshing(true);
                if (mFragmentType == 0){
                    mPresenter.getPostData(Constant.FRAGMENT_TAG, null, null, mPage, mTag);
                }else {
                    mPresenter.getPostData(mFragmentType, mLongitude, mLatitude, mPage, null);
                }
            }
        });
    }

    @Override
    public void getPostDataSuccess(List<NeighborRecommand> list) {
        if (mSrlRefresh != null && mSrlRefresh.isRefreshing()){
            mSrlRefresh.setRefreshing(false);
        }
        if (mPage == 1) {
            mRecommandList = list;
            mRecommandAdapter.updateData(mRecommandList);
        } else {
            mRecommandList.addAll(list);
            mRecommandAdapter.appendData(list);
        }
    }

    @Override
    public void getPostDataError(String msg) {
        Log.e(TAG, "getPostDataError: " + msg);
        showToast(msg);
    }
}
